//
//  YZMTutorialViewController.m
//  YZMicroStock
//
//  Created by wangminhong on 16/3/22.
//  Copyright © 2016年 cqjr. All rights reserved.
//

#import "YZMTutorialViewController.h"
#import "YZMPageControl.h"
#import "YZMControllerManager.h"

@interface YZMTutorialViewController ()<UIScrollViewDelegate>

@end

@implementation YZMTutorialViewController

{
    
    UIScrollView *_scrollView;
    UIView *_containerView;
    YZMPageControl *_pageControl;
}

- (void)viewDidLoad{
    
    [super viewDidLoad];
    self.view.backgroundColor = [YZMAppStyle appBackgroundColor];
    
    [self createObjects];
}

- (void)createObjects{
    
    _scrollView = [UIScrollView new];
    _scrollView.pagingEnabled = YES;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.delegate = self;
    _scrollView.pagingEnabled = YES;
    _scrollView.bounces  = NO;
    _scrollView.userInteractionEnabled=YES;
    [self.view addSubview:_scrollView];
    [_scrollView mas_makeConstraints:^(MASConstraintMaker *maker){
        
        maker.edges.equalTo(self.view);
        
    }];
    
    _containerView = [UIView new];
    [_scrollView addSubview:_containerView];
    [_containerView mas_makeConstraints:^(MASConstraintMaker *maker){
        
        maker.edges.equalTo(_scrollView);
        maker.height.equalTo(_scrollView);
    }];
    
    UIView *lastView = nil;
    for (int i=0; i<5; i++) {
        
        UIImageView *imageView = [UIImageView new];
        imageView.image = [self getImageName:i];
        imageView.userInteractionEnabled = YES;
        [_containerView addSubview:imageView];
        [imageView mas_makeConstraints:^(MASConstraintMaker *maker){
            
            maker.top.bottom.equalTo(_containerView);
            maker.width.equalTo(self.view);
            
            if (!lastView) {
                
                maker.left.equalTo(_containerView);
            }else{
                
                maker.left.equalTo(lastView.mas_right);
            }
        }];
        
        lastView = imageView;
        if (i == 4) {
            UIView *panView=[UIView new];
            [imageView addSubview:panView];
            [panView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.top.bottom.equalTo(imageView);
                make.width.mas_equalTo(ScreenWidth/2);
            }];
            panView.userInteractionEnabled=YES;
            
//            UIPanGestureRecognizer *swipeGst=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(lastImage:)];

            
//            [panView addGestureRecognizer:swipeGst];
        }
    }
    
    [_containerView mas_makeConstraints:^(MASConstraintMaker *maker){
        
        maker.right.equalTo(lastView);
    }];
    
    UIButton *button = [UIButton new];
    [button setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.4]];
    button.layer.cornerRadius = 10;
    button.layer.masksToBounds = YES;
    button.layer.borderColor = [UIColor clearColor].CGColor;
    button.layer.borderWidth = 1;
    [button setTitle:@"进入" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button.titleLabel setFont:[YZMAppStyle mediumSizeFont]];
    [button addTarget:self action:@selector(beginUse) forControlEvents:UIControlEventTouchUpInside];
    [lastView addSubview:button];
    [button mas_makeConstraints:^(MASConstraintMaker *maker){
        
        maker.top.equalTo(lastView).offset(15);
        maker.right.equalTo(lastView).offset(-15);
        maker.width.mas_equalTo(60);
        maker.height.mas_equalTo(30);
    }];
    
    _pageControl = [[YZMPageControl alloc] init];
    _pageControl.numberOfPages = 5;
    _pageControl.currentPage = 0;
    _pageControl.normalColor = [UIColor grayColor];
    _pageControl.highlightColor = [UIColor lightGrayColor];
    [self.view addSubview:_pageControl];
    [_pageControl mas_makeConstraints:^(MASConstraintMaker *maker){
        
        maker.bottom.equalTo(self.view);
        maker.centerX.equalTo(self.view);
    }];
}

-(void)lastImage:(UIPanGestureRecognizer *)sender{
    CGFloat translateX=[sender translationInView:sender.view].x;
    if (translateX<0) {
        [self beginUse];
    }
}

-(UIImage *)getImageName:(int)index
{
    CGRect rect = [UIScreen mainScreen].bounds;
    float screenHeight = rect.size.height;
    
    UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"splash_%d_%.0fh",index+1, screenHeight]];
    if (!image) {
        
        image = [UIImage imageNamed:[NSString stringWithFormat:@"splash_%d_667h",index+1]];
    }
    return image;
}

- (void)beginUse{
    
    /**设置第一次使用标志为False**/
    [[NSUserDefaults standardUserDefaults] setBool:true forKey:sYZMUnShowGuidePageFlagDefault];
    
    /**保存当前版本号**/
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    [[NSUserDefaults standardUserDefaults] setObject:[infoDictionary objectForKey:@"CFBundleVersion"] forKey:sYZMAppCurrentBuildVersionDefault];
    
    [YZMCMInstance moveToMain];
    
}

- (void)scrollViewDidScroll:(UIScrollView *)sender {
    
    int page = sender.contentOffset.x / sender.frame.size.width;
    
    _pageControl.currentPage = page;
    
}

- (BOOL)prefersStatusBarHidden
{
    return YES;
}

@end
